package config

import (
	"cirrus-core/config/global_vars"
	"gopkg.in/ini.v1"
	"log"
)

type ServiceConfig struct {
	ServiceName  string `ini:"service_name"`
	Port         string `ini:"port"`
	RegistryAddr string `ini:"registry_addr"`
	RegistryNamespace string `ini:"registry_namespace"`
	RegistryGroup string `ini:"registry_group"`
	Mode         string `ini:"mode"`
	Version      string `ini:"version"`
}

var serviceConfig = new(ServiceConfig)
func GetServiceConfig() *ServiceConfig {
	return serviceConfig
}

func GetConfigFile() string {
	resourcesPath := *global_vars.ResourcesPath
	path := resourcesPath + "/service.ini"
	return path
}

func InitConfig() *ServiceConfig {
	path := GetConfigFile()
	cfg, err := ini.Load(path)
	if err != nil {
		log.Fatal("InitConfig: ", path, ".  config err: ", err.Error())
	}

	err = cfg.MapTo(serviceConfig)

	if err != nil {
		log.Fatal("InitConfig: ", path, ".  MapTo err: ", err.Error())
	}
	return serviceConfig
}
